package fr.umlv.m2.uwp2011.web.client.professor;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.OpenEvent;
import com.google.gwt.event.logical.shared.OpenHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.umlv.m2.uwp2011.service.matiere.DisplayCursus;
import fr.umlv.m2.uwp2011.service.matiere.ICursusService;
import fr.umlv.m2.uwp2011.service.matiere.ICursusServiceAsync;
import fr.umlv.m2.uwp2011.web.client.Application;
import fr.umlv.m2.uwp2011.web.client.Session;

public class ProfessorMenu extends Composite {

  /**
   * Home page link.
   */
  private final Hyperlink home;
  /**
   * Disconnect link.
   */
  private final Hyperlink disconnect;

  /**
   * Planning menu.
   */
  private final DisclosurePanel planningMenu;
  /**
   * Teaching menu.
   */
  private final DisclosurePanel teachingMenu;
  /**
   * List of student menu.
   */
  private final DisclosurePanel studentListMenu;

  /**
   * Planning sub menu panel. 
   */
  private final VerticalPanel planningSubMenu;
  /**
   * Teaching sub menu panel. 
   */
  private final VerticalPanel teachingSubMenu;
  /**
   * List of student sub menu panel. 
   */
  private final VerticalPanel studentListSubMenu;

  /**
   * Menu Panel.
   */
  private final VerticalPanel menuPanel;
  /**
   * Main panel.
   */
  private final VerticalPanel menu;

  /**
   * rpc cursus for collecting informations about cursus.
   */
  private final ICursusServiceAsync cursusService;

  public ProfessorMenu(Session session) {
    /****************************** Widgets Creation ******************************/
    this.cursusService = GWT.create(ICursusService.class);
    
    this.home = new Hyperlink(
        "Accueil", Application.PROFESSOR_HOMEPAGE + 
        "?login=" + session.getParameters().get("login") +
        "&key=" + session.getParameters().get("key")
    );
    this.disconnect = new Hyperlink(
        "D&eacute;connexion", true, Application.LOGIN_PAGE
    );

    this.planningMenu = new DisclosurePanel("Emploi du temps");
    this.teachingMenu = new DisclosurePanel("Enseignement");
    this.studentListMenu = new DisclosurePanel("Liste des étudiant");

    this.planningSubMenu = new VerticalPanel();
    this.teachingSubMenu = new VerticalPanel();
    this.studentListSubMenu = new VerticalPanel();

    this.menuPanel = new VerticalPanel();
    this.menu = new VerticalPanel();

    /*************************** Widgets initialization ***************************/
    this.planningSubMenu.add(new Hyperlink(
        "Cours",
        Application.PROFESSOR_PLANNING_COURSE + "?login=" + session.getParameters().get("login") +
        "&key=" + session.getParameters().get("key")
    ));
    this.planningSubMenu.add(new Hyperlink(
        "Rendu de travaux",
        Application.PROFESSOR_PLANNING_WORKS + "?login=" + session.getParameters().get("login") +
        "&key=" + session.getParameters().get("key")
    ));

    createTeachingSubMenu(session);
    createStudentListSubMenu(session);

    this.planningMenu.add(planningSubMenu);
    this.teachingMenu.add(teachingSubMenu);
    this.studentListMenu.add(studentListSubMenu);

    /******************************** Widgets style *******************************/
    this.home.addStyleName("menuLink");
    this.disconnect.addStyleName("menuLink");

    this.planningMenu.setWidth("100%");
    this.planningMenu.setHeader(new Label("Emploi du temps"));
    this.planningMenu.setAnimationEnabled(true);
    this.planningMenu.addStyleName("menuLink");

    this.teachingMenu.setWidth("100%");
    this.teachingMenu.setHeader(new Label("Enseignement"));
    this.teachingMenu.setAnimationEnabled(true);
    this.teachingMenu.addStyleName("menuLink");

    this.studentListMenu.setWidth("100%");
    this.studentListMenu.setHeader(new Label("Liste des étudiants"));
    this.studentListMenu.setAnimationEnabled(true);
    this.studentListMenu.addStyleName("menuLink");

    /******************************** Page building *******************************/
    this.menuPanel.add(home);
    this.menuPanel.add(planningMenu);
    this.menuPanel.add(teachingMenu);
    this.menuPanel.add(studentListMenu);
    this.menuPanel.add(disconnect);

    this.menu.add(this.menuPanel);

    initWidget(this.menu);

    /******************************* Action handler *******************************/
    this.planningMenu.addOpenHandler(new OpenHandler<DisclosurePanel>() {

      @Override
      public void onOpen(OpenEvent<DisclosurePanel> event) {
        teachingMenu.setOpen(false);
        studentListMenu.setOpen(false);
      }
    });

    this.teachingMenu.addOpenHandler(new OpenHandler<DisclosurePanel>() {

      @Override
      public void onOpen(OpenEvent<DisclosurePanel> event) {
        studentListMenu.setOpen(false);
        planningMenu.setOpen(false);
      }
    });

    this.studentListMenu.addOpenHandler(new OpenHandler<DisclosurePanel>() {

      @Override
      public void onOpen(OpenEvent<DisclosurePanel> event) {
        planningMenu.setOpen(false);
        teachingMenu.setOpen(false);
      }
    });
  }

  /**
   * create the list of cursus of the professor user for TeachingSubMenu
   * @param user professor
   */
  private void createTeachingSubMenu(final Session session) {
    cursusService.getListCursusByUser(session.getUser().getLogin(), new AsyncCallback<List<DisplayCursus>>() {

      @Override
      public void onSuccess(List<DisplayCursus> listCursus) {
        for(DisplayCursus cursus : listCursus) {
          final Hyperlink link = new Hyperlink(
              cursus.getLibelle(),
              Application.PROFESSOR_TEACHING + "?login=" + session.getParameters().get("login") +
              "&key=" + session.getParameters().get("key") +
              "&cursus=" + cursus.getId()
          );
          teachingSubMenu.add(link);
        }
      }
      @Override
      public void onFailure(Throwable arg0) {}
    });
  }

  /**
   * create the list of cursus of the professor user for listStudentListSubMenu
   * @param user professor
   */
  private void createStudentListSubMenu(final Session session) {
    cursusService.getListCursusByUser(session.getUser().getLogin(), new AsyncCallback<List<DisplayCursus>>() {

      @Override
      public void onSuccess(List<DisplayCursus> listCursus) {
        for(DisplayCursus cursus : listCursus) {
          final Hyperlink link = new Hyperlink(
              cursus.getLibelle(),
              Application.PROFESSOR_STUDENTS + "?login=" + session.getParameters().get("login") +
              "&key=" + session.getParameters().get("key") +
              "&cursus=" + cursus.getId()
          );
          studentListSubMenu.add(link);
        }
      }
      @Override
      public void onFailure(Throwable arg0) {}
    });
  }
}
